#!/usr/bin/python

import json
import sys
import pickle
import numpy as np

PH = None
CH = None

def main():
	#printTree()
	childHash()
	parentHash()
	LabelByLayer()

def childHash():
	with open('../AmazonHeirarchy.json') as f:
		
		CH = dict()		
		j = json.load(f)

		parents = []
		CH[0] = []
		for n in j:
			parents.append(n)
			CH[0].append(int(n['BrowseNodeId']))

		while len(parents)!= 0:
			children = []
			for p in parents:
				if 'Children' in p:
					for c in p['Children']:
						children.append(c)
						if int(p['BrowseNodeId']) not in CH:
							CH[int(p['BrowseNodeId'])] = []
						CH[int(p['BrowseNodeId'])].append(int(c['BrowseNodeId']))
			parents = children

		for k in CH.keys():
			CH[k] = list(set(CH[k]))

		with open('../CH.pkl', 'wb') as outfile:
			pickle.dump(CH, outfile)

def parentHash():
	with open('../AmazonHeirarchy.json') as f:
		
		global PH
		PH = dict()
		j = json.load(f)

		parents = []
		for n in j:
			parents.append(n)
			PH[int(n['BrowseNodeId'])] = [0]

		while len(parents)!= 0:
			children = []
			for p in parents:
				if 'Children' in p:
					for c in p['Children']:
						children.append(c)
						if int(c['BrowseNodeId']) not in PH:
							PH[int(c['BrowseNodeId'])] = []
						PH[int(c['BrowseNodeId'])].append(int(p['BrowseNodeId']))
			parents = children


		for k in PH.keys():
			PH[k] = list(set(PH[k]))
		with open('../PH.pkl', 'wb') as outfile:
			pickle.dump(PH, outfile)
			
		#print PH
	
def LabelByLayer():
	
	with open('../AmazonHeirarchy.json') as f:
		
		j = json.load(f)

		parents = []		
		for n in j:
			parents.append(n)
		layer = 1

		allLabels = []

		while len(parents)!= 0:
			validLabels = []
			children = []
			for n in parents:
				validLabels.append(int(n['BrowseNodeId']))
				if 'Children' in n:
					for c in n['Children']:
						children.append(c)

			allLabels += validLabels
			validLabels = np.array(validLabels)
			np.savetxt('../validLabels_L'+str(layer)+'.txt',np.array(validLabels), fmt='%d')
			print 'Layer ',layer,': ',len(validLabels)," Labels"
			parents = children
			layer += 1

		allLabels = list(set(allLabels))
		np.savetxt('../validLabels.txt',np.array(allLabels), fmt='%d')

def printTree():
	with open('../AmazonHeirarchy.json') as f:
		j = json.load(f)

		validLabels = []
			
		for i in j:
			printNode(i,0,'')
			

def printNode(n,depth,p):
	#for i in range(depth):
	#	sys.stdout.write('\t');
	name = p + n['Name'] + ' # ('+n['BrowseNodeId']+')'
	print name.encode('utf-8')
	name = p + n['Name'] + ' > '
	if 'Children' in n:
		for i in n['Children']:
			printNode(i,depth+1,name)
			pass


if __name__ == "__main__":
    main()
